package com.myidis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auxiliary.UserListForm;
import com.myidis.entity.User;
import com.myidis.request.PasswordReq;
import com.myidis.response.UserRes;
import com.myidis.servlet.UserServlet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {
	@Autowired
	private UserServlet userServlet;
	
	@ResponseBody
	@ApiOperation("获取符合条件用户的可视化列表")
	@RequestMapping(value="/searchList",method=RequestMethod.POST)
	public UserListForm searchList(@RequestBody User search) {
		return userServlet.search(search);
	}
	
	@ResponseBody
	@ApiOperation("获取所有用户的可视化列表")
	@RequestMapping(value="/listForm",method=RequestMethod.POST)
	public UserListForm listFrom() {
		return userServlet.form();
	}
	
	@ResponseBody
	@ApiOperation("获取一个用户的信息")
	@RequestMapping(value="/entity",method=RequestMethod.POST)
	public User one(@RequestBody User user) {
		return userServlet.getOne(user.getId());
	}
	
	@ResponseBody
	@ApiOperation("通过用户名获取一个用户的信息")
	@RequestMapping(value="/entity/other",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public User oneByOther(User req) {
		if(req.getUserName() != null)
			return userServlet.getByUN(req.getUserName());
		else
			return null;
	}
	
	@ResponseBody
	@ApiOperation("添加一个用户")
	@RequestMapping(value="/add",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int add(@RequestBody User user) {
		return userServlet.insert(user);
	}
	
	@ResponseBody
	@ApiOperation("获取一组用户")
	@RequestMapping(value="addAll",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int addAll(@RequestBody List<User> users) {
		return userServlet.insertAll(users);
	}
	
	@ResponseBody
	@ApiOperation("修改一个用户")
	@RequestMapping(value="upd",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int upd(@RequestBody UserRes user) {
		return userServlet.update(user.useful());
	}
	
	@ResponseBody
	@ApiOperation("修改用户密码")
	@RequestMapping(value="psw",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int psw(@RequestBody PasswordReq req) {
		return userServlet.pswUpd(req);
	}

	@ResponseBody
	@ApiOperation("删除一个用户")
	@RequestMapping(value="del",method=RequestMethod.POST)
	public int del(@RequestBody int id) {
		return userServlet.delete(id);
	}
	
	@ResponseBody
	@ApiOperation("删除一组用户")
	@RequestMapping(value="delAll",method=RequestMethod.POST)
	public int delAll(@RequestBody int[] ids) {
		return userServlet.deleteAll(ids);
	}
}
